//
//  NetRequest.m
//  iNews
//
//  Created by lanou3g on 14-12-4.
//  Copyright (c) 2014年 张在青. All rights reserved.
//7

#import "NetRequest.h"
#import "AppDelegate.h"

#import "Reachability.h"

@implementation NetRequest

//(get异步)网络请求数据,通过block块回调
+ (void)netReuestWithUrlString:(NSString *)urlString block:(myBlock)mb
{
    /*
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [[[NSURLRequest alloc] initWithURL:url] autorelease];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
                 
        if ([data length] > 0 && connectionError == nil) {
            mb(data);
        }else if ([data length] == 0 && connectionError == nil){
            NSLog(@"Nothing was downloaded.");
        }else if (connectionError != nil){
           
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络不给力" message:@"请检查网络连接" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            
            [alert show];
            [alert dismissWithClickedButtonIndex:1 animated:YES];

            [alert release];
        }
    }];
    */

    AppDelegate *app = [UIApplication sharedApplication].delegate;
  
    //加缓存的网络请求
    if ([urlString length] == 0) {
        NSLog(@"空的url");
    }

    NSURLCache *urlCache = [NSURLCache sharedURLCache];
    //设置缓存大小2M
    [urlCache setMemoryCapacity:2 * 1024 * 1024];
    
    NSURL *url = [NSURL URLWithString:urlString];
    //分别是url路径,缓存协议,网络请求超过时间
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0f];
    
    //从请求中获取缓存输出
    NSCachedURLResponse *response = [urlCache cachedResponseForRequest:request];                              
    //判断是否有缓存
    if (response != nil) {
      //  NSLog(@"有缓存");
        [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
    }
    

//    if (app.net) {
    
    
    //创建请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
       
        
        if ([data length] > 0 && connectionError == nil) {
           
            mb(data);

     
          }else if ([data length] == 0 && connectionError == nil){

            UIAlertView *aV = [[UIAlertView alloc] initWithTitle:nil message:@"网络不给力" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [aV show];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [aV dismissWithClickedButtonIndex:0 animated:YES];
                [aV release];
            });
            
        }else{
            
            UIAlertView *aV = [[UIAlertView alloc] initWithTitle:@"请求数据失败" message:@"请检查网络连接" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [aV show];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [aV dismissWithClickedButtonIndex:0 animated:YES];
                [aV release];
            });
            
        }
    }];
        
//    }else{
//        
//        UIAlertView *aV = [[UIAlertView alloc] initWithTitle:@"请求数据失败" message:@"请检查网络连接" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
//        [aV show];
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [aV dismissWithClickedButtonIndex:0 animated:YES];
//            [aV release];
//        });
//
    
    
}











@end
